<template>
  <div>
    <div class="flex justify-end items-center mt-2">
      <a-button type="primary" class="mr-2" @click="onClickAdd()">新增申请单</a-button>
    </div>
    <template v-if="applyList.length">
      <div style="height: calc(100vh - 260px)" class="mt-2 px-1.5 overflow-auto">
        <div v-for="item in applyList" :key="item.lsh" class="mt-5 mb-2 border border-solid">
          <div class="h-12 border-b border-solid flex items-center bg-thin">
            <div class="name truncate">
              <LabelValue class="ml-10" :width="200" tooltip :value="item?.categroyName" />
            </div>
            <div>
              <a-tag v-if="item.status === '0'" color="blue">未审核</a-tag>
              <a-tag v-if="item.status === '1'" color="green">已审核</a-tag>
              <a-tag v-if="item.status === '2'" color="red">已作废</a-tag>
            </div>
            <LabelValue class="ml-12" label="科室" tooltip :width="180" :value="item?.applyDeptName" />
            <LabelValue class="ml-1" label="申请医生" tooltip :width="150" :value="item?.applyDoctorName" />
            <LabelValue class="ml-12" label="申请时间" tooltip :width="227" :value="item?.applyTime" />
          </div>
          <div class="h-16 flex items-center justify-between">
            <div class="flex">
              <LabelValue class="ml-12 mr-8" tooltip :width="420" :value="item.items" />
              <LabelValue class="ml-12" label="审核人" tooltip :width="120" :value="item?.createUserName" />
            </div>

            <div class="pr-4">
              <a-button type="link" size="small" :disabled="item.status === '1' || item.status === '2'" @click="onClickAdd(item)"> 修改 </a-button>
              <a-divider type="vertical" />
              <BaseButton
                confirm-text="确认作废?"
                type="text"
                danger
                size="small"
                :disabled="item.status === '1' || item.status === '2'"
                @click="onDelete(item)"
              >
                作废
              </BaseButton>
              <template v-if="item.status === '1'">
                <a-divider type="vertical" />
                <a-button type="link" size="small" @click="onPrint(item, 1)"> 查阅报告 </a-button>
                <a-divider type="vertical" />
                <a-button type="link" size="small" @click="onPrint(item, 2)"> 打印单据 </a-button>
              </template>
            </div>
          </div>
        </div>
      </div>
    </template>
    <template v-else>
      <a-empty />
    </template>
  </div>
  <Edit v-model:open="open" :table-row="tableRow" :row-data="store.currentPatient" :patient-data="patientData" @success="queryList()" />
</template>
<script setup>
import { ref, watchEffect } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'
import LabelValue from '@/components/LabelValue'
import userStore from '@/store/user'
import useStore from '../store'
import Edit from './Edit'

defineProps({
  patientData: {
    type: Object,
    default: () => ({})
  }
})

const { empId } = userStore()

const store = useStore()
const open = ref(false)
const applyList = ref([])
const tableRow = ref({})
const onClickAdd = (row = {}) => {
  tableRow.value = { ...row, key: store.currentPatient.jzbh, applyDoctorId: empId, brId: store.currentPatient.brId }
  open.value = true
}

const queryList = async () => {
  const { code, data, msg } = await post('/lis/LisApply/getLisApplyList', {
    pyjm: '',
    key: store.currentPatient.jzbh,
    applyPath: 0
  })
  if (code !== 0) return message.error(msg)
  applyList.value = data
}

const onDelete = async (row) => {
  const { code, msg } = await post('/lis/LisApply/doCancelLisApply', { lsh: row.lsh })
  code ? message.error(msg) : message.success(msg)
  queryList()
}
// 打印
const onPrint = async (item, dylx) => {
  // /lis/lisPrint/printLisApply
  const { code, msg, data } = await post('/lis/lisPrint/printLisRecord', { lsh: item.lsh, dylx })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}

watchEffect(() => {
  if (store.currentPatient.jzbh) {
    queryList()
  }
})
</script>
<style lang="less" scoped>
.name {
  padding: 0 5px;
  font-weight: 700;
  font-size: 20px;
}
</style>
